package cn.wanda.bi.sunshine.platform.server.aspect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.util.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import cn.wanda.bi.sunshine.platform.server.SunshineCode;
import cn.wanda.bi.sunshine.platform.server.bean.mapper.UserLabelReturnFieldMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelReturnField;
import cn.wanda.bi.sunshine.platform.server.bean.model.UserLabelReturnFieldExample;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelHitsDetail;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import cn.wanda.bi.sunshine.platform.server.service.UserLabelReturnFieldService;
import cn.wanda.bi.sunshine.platform.server.utils.MapUtil;
import cn.wanda.bi.sunshine.platform.server.utils.exception.SunshineException;

/**
 * Created by wangfupeng1 on 2017-5-6.
 */
@Aspect
@Component
@Order(2)
public class AuthLabelListAspect {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserLabelReturnFieldService userLabelReturnFieldService;
    
    @Around("@annotation(cn.wanda.bi.sunshine.platform.server.aspect.AuthLabelList)")
    public Object aroundLabel(ProceedingJoinPoint pjp) throws Throwable {
        Object ob = pjp.proceed();
        ListResponse listResponse = (ListResponse) ob;
        List<UserLabelHitsDetail> userLabelHitsDetails= (List<UserLabelHitsDetail>) listResponse.getData();
        String appid = request.getHeader("appid");
    	UserLabelReturnFieldExample dimUserLabelAllReturnFieldExample = new UserLabelReturnFieldExample();
    	dimUserLabelAllReturnFieldExample.createCriteria().andAppidEqualTo(appid);
    	List<UserLabelReturnField> dimUserLabelAllReturnFields= userLabelReturnFieldService.getUserLabelReturnFields(dimUserLabelAllReturnFieldExample, appid);
    	if(CollectionUtils.isEmpty(dimUserLabelAllReturnFields))
    		throw new SunshineException(SunshineCode.APPID_WRONG_OR_secret_WRONG);
        List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
        userLabelHitsDetails.forEach(hit->{
			Map<String,Object> hitMap = (Map<String,Object>)MapUtil.objToMap(hit.get_source());
    		Map<String,Object> hitResult = new HashMap<String,Object>();
    		dimUserLabelAllReturnFields.forEach(dimUserLabelAllReturnField->{
    			String field = dimUserLabelAllReturnField.getReturnField();
    			hitResult.put(field, hitMap.get(field));
    		});
    		lists.add(hitResult);
    	});
        return ListResponse.success(lists, listResponse.getTotal(), listResponse.getPage(), listResponse.getPageSize());
    }
}
